package cn.tedu.image.util;

import cn.tedu.image.mapper.ImagePathMapper;
import cn.tedu.image.mapper.ImageRoMapper;
import cn.tedu.image.pojo.ImagePath;
import cn.tedu.image.pojo.ImageRo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 本类用于
 *
 * @author SJXQ
 * @version 2022/1/17 15:08
 */
@Slf4j
@Component
public class ImageUtils {
    @Autowired
    private ImageRoMapper imageMapper;
    @Autowired
    private ImagePathMapper pathMapper;
    private static List<String> images = new ArrayList<>();
    private static final String IMAGE_FORMATS = "jpeg,jpg,png,gif";

    public static void main(String[] args) {

    }

    public void scanDirectorysOfImageByParentDir(String parentDirectory) {
        scanDirectorysOfImageByParentDir(parentDirectory, false);
    }

    public void scanDirectorysOfImageByParentDir(String parentDirectory, Boolean recurve) {
        File file = new File(parentDirectory);
        if (!file.exists()) {
            log.error("在扫描图片文件夹：{} 时发现此路径不存在，请检查", parentDirectory);
            return;
        }
        if (!file.isDirectory()) {
            file = file.getParentFile();
        }
        ImagePath imagePath = null;
        for (File listFile : file.listFiles()) {
            if (listFile.isDirectory()) {
                // 如果开启递归扫描的话，也会扫描子文件夹
                if (recurve) {
                    scanDirectorysOfImageByParentDir(listFile.getAbsolutePath());
                }
            } else if(listFile.isFile()) {
                String imageFormat = StringUtils.getFilenameExtension(listFile.getAbsolutePath());
                if (imageFormat != null) {
                    if (IMAGE_FORMATS.indexOf(imageFormat) > -1 && listFile.length() > 1024 * 500) {
                        if (imagePath == null) {
                            imagePath = new ImagePath();
                            imagePath.setPath(file.getAbsolutePath());
                            QueryWrapper<ImagePath> qr = new QueryWrapper<>();
                            qr.eq("path", imagePath.getPath());
                            final List<ImagePath> imagePaths = pathMapper.selectList(qr);
                            if (imagePaths.isEmpty()) {
                                pathMapper.insert(imagePath);
                                System.out.println("插入数据库后:"+imagePath);
                            }else{
                                imagePath = imagePaths.get(0);
                                System.out.println("从数据库中查询到的："+imagePath);
                            }
                        }
                        if (imagePath.getId() != null) {
                            ImageRo imageRo = new ImageRo();
                            imageRo.setParentId(imagePath.getId());
                            imageRo.setFileName(listFile.getName());
                            imageRo.setHash(listFile.hashCode());
                            QueryWrapper<ImageRo> imageRoQueryWrapper = new QueryWrapper<>();
                            imageRoQueryWrapper.eq("hash", imageRo.getHash());
                            if (imageMapper.selectList(imageRoQueryWrapper).isEmpty()) {
                                imageMapper.insert(imageRo);
                            }
                        }else{
                            log.error("出错了，imagePath不为null，但是getId()却返回null,出错的imagePath:{}",imagePath);
                        }
                    }
                }
            }
        }
    }

}
